﻿using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Simple.Services.System.ClassRoom;

namespace Simple.WebApi.Controllers.System;

/// <summary>
/// 园所信息和教室管理
/// </summary>
[Route("api/GardenHouse/[action]")]
[ApiController]
[Authorize]
public class GardenHouseController : ControllerBase
{
    private readonly ClassRoomService _classRoomService;

    private readonly GardenHuoseService _gardenHuoseService;

    public GardenHouseController(ClassRoomService classRoomService, GardenHuoseService gardenHuoseService)
    {
        _classRoomService = classRoomService;
        _gardenHuoseService = gardenHuoseService;
    }

    /// <summary>
    /// 查询教室详情
    /// </summary>
    /// <param name="input"></param>
    /// <returns></returns>
    [HttpGet]
    public async Task<AppResult> GetClassRoomInfoByRoomId([FromQuery] ClassRoomSingleInputModel input)
    {
        var data = await _classRoomService.GetAsync(input);
        return AppResult.Status200OK(data: data);
    }

    /// <summary>
    /// 教室列表分页查询
    /// </summary>
    /// <param name="model"></param>
    /// <returns></returns>
    [HttpGet]
    public async Task<AppResult> Page([FromQuery] ClassRoomPageInputModel model)
    {
        var data = await _classRoomService.GetPageAsync(model);
        return AppResult.Status200OK(data: data);
    }

    /// <summary>
    /// 教室列表
    /// </summary>
    /// <param name="model"></param>
    /// <returns></returns>
    [HttpGet]
    public async Task<AppResult> List()
    {
        var data = await _classRoomService.GetAll();
        return AppResult.Status200OK(data: data);
    }

    /// <summary>
    /// 增加教室
    /// </summary>
    /// <param name="model"></param>
    /// <returns></returns>
    [HttpPost]
    public async Task<AppResult> Add([FromBody] ClassRoomOutputModel model)
    {
        await _classRoomService.AddAsync(model);
        return AppResult.Status200OK("新增成功");
    }

    /// <summary>
    /// 编辑教室
    /// </summary>
    /// <param name="model"></param>
    /// <returns></returns>
    [HttpPost]
    public async Task<AppResult> Edit([FromBody] ClassRoomOutputModel model)
    {
        await _classRoomService.UpdateAsync(model);
        return AppResult.Status200OK("更新成功");
    }

    /// <summary>
    /// 删除教室
    /// </summary>
    /// <param name="model"></param>
    /// <returns></returns>
    [HttpPost]
    public async Task<AppResult> Delete([FromBody] List<ClassRoomSingleInputModel> model)
    {
        await _classRoomService.DeleteAsync(model?.Select(c => c.CRoomId)?.ToList());
        return AppResult.Status200OK("删除成功");
    }

    /// <summary>
    /// 获取园所信息
    /// </summary>
    /// <param name="model"></param>
    /// <returns></returns>
    [HttpGet]
    public async Task<AppResult> Info()
    {
        var data = await _gardenHuoseService.GetGardenHuoseAsync();
        return AppResult.Status200OK(data: data);
    }

    /// <summary>
    /// 保存园所信息
    /// </summary>
    /// <param name="model"></param>
    /// <returns></returns>
    [HttpPost]
    public async Task<AppResult> Save([FromBody] GardenHuoseOutputModel model)
    {
        var data = await _gardenHuoseService.SaveGardenHuoseAsync(model);
        return AppResult.Status200OK(data: data);
    }

    /// <summary>
    /// 创建园所二维码
    /// </summary>
    /// <param name="model"></param>
    /// <returns></returns>
    [HttpPost]
    public async Task<AppResult> QRCode([FromBody] GardenHuoseQrCodeImageModel model)
    {
        var data = await _gardenHuoseService.GetGardenHuoseQrCode(model.Content, model.With, model.Height);
        return AppResult.Status200OK(data: data);
    }

    /// <summary>
    /// 获取省市区数据
    /// </summary>
    /// <returns></returns>
    [HttpGet]
    public async Task<AppResult> ProvincesAll()
    {
        var data = await _gardenHuoseService.GetProvincesByLocationFile();
        return AppResult.Status200OK(data: data);
    }

    /// <summary>
    /// 更改教室状态
    /// </summary>
    /// <param name="model"></param>
    /// <returns></returns>
    [HttpPost]
    public async Task<AppResult> ChangeStatus([FromBody] ClassRoomChangeStatus model)
    {
        await _classRoomService.ChangeStatus(model);
        return AppResult.Status200OK("状态更改成功！");
    }
}
